package fm.dongman.animefm.view.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import fm.dongman.animefm.R
import fm.dongman.animefm.view.adapter.FindPagerAdapter
import fm.dongman.animefm.view.transform.DepthPageTransformer
import fm.dongman.animefm.view.util.SystemHelper
import kotlinx.android.synthetic.main.fragment_find.*

/**
 * 发现页面
 * Created by shize on 2017/9/28.
 */
class FindFragment : BaseFragment() {
    private lateinit var mTabTitle: Array<String> // tab 标题
    private lateinit var mFragments: Array<BaseFragment> // tab 页面

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val fragment = inflater?.inflate(R.layout.fragment_find, container, false)

        return fragment!!
    }

    override fun initData() {
        mTabTitle = arrayOf(getString(R.string.str_find_article), getString(R.string.str_find_sheet),
                getString(R.string.str_find_rank))
        mFragments = arrayOf(FArticleFragment.getInstance(), FSheetFragment.getInstance(),
                FRankFragment.getInstance())
    }

    override fun bindData() {
        mTabTitle.forEach {
            mFindTabLayout.addTab(mFindTabLayout.newTab().setText(it))
        }
        mFindViewPager.adapter = FindPagerAdapter(this.childFragmentManager, mTabTitle, mFragments)
        mFindViewPager.offscreenPageLimit = 2
        mFindViewPager.setPageTransformer(true, DepthPageTransformer())
        mFindTabLayout.setupWithViewPager(mFindViewPager)
        mTbFind.setPadding(0, SystemHelper.getStatusBarHeight(activity), 0, 0)
    }

    companion object {
        private var FRAGMENT_INSTANCE: FindFragment? = null

        /**
         * 获取实例
         */
        fun getInstance(): FindFragment {
            if (FRAGMENT_INSTANCE == null) {
                FRAGMENT_INSTANCE = FindFragment()
            }
            return FRAGMENT_INSTANCE!!
        }

        /**
         * 销毁实例
         */
        fun destroyInstance() {
            FRAGMENT_INSTANCE = null
        }
    }
}